package com.cwiyc.orderonlineback.service;

import com.cwiyc.orderonlineback.common.Result;
import com.cwiyc.orderonlineback.entity.Meal;
import com.cwiyc.orderonlineback.mapper.MealMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MealServiceImpl {
    @Resource
    MealMapper mealMapper;

    public Result<?> getMeal(@RequestBody Meal meal) {
        List<Meal> res = mealMapper.getmeal(meal.getMsid());
        if (res == null) {
            return Result.error("-1", "菜单不存在");
        }
        return Result.success(res);
    }

    public Result<?> deleteMeal(@RequestBody Integer mid) {
        int res = mealMapper.deletemeal(mid);
        if (res == 0) {
            return Result.error("-1", "删除餐品失败");
        }
        return Result.success();
    }

    public Result<?> insertMeal(@RequestBody Meal meal) {
        int res = mealMapper.insertmeal(meal.getMname(), meal.getMintro(), meal.getMpic(), meal.getMprice(), meal.getMstock(),meal.getMsales(), meal.getMstatus(), meal.getMtype(), meal.getMsid());
        if (res == 0) {
            return Result.error("-1", "上架餐品失败");
        }
        return Result.success();
    }

    public Result<?> modeifyMeal(@RequestBody Meal meal) {
        int res = mealMapper.modeifymeal(meal.getMid(), meal.getMname(), meal.getMintro(), meal.getMpic(), meal.getMprice(), meal.getMstock(),meal.getMsales(), meal.getMstatus(), meal.getMtype(), meal.getMsid());
        if (res == 0) {
            return Result.error("-1", "修改餐品失败");
        }
        return Result.success();
    }

    public Result<?> getMealByCode(String code) {
        List<Meal> res = mealMapper.getmealbycode(code);
        if (res == null) {
            return Result.error("-1", "菜单不存在");
        }
        return Result.success(res);
    }
}
